package chapter15;

import chapter15.generator.Coffee;
import chapter15.generator.CoffeeGenerator;
import chapter15.generator.Generator;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author : DuJiabao
 * @Date : 2021/3/4 15:25
 * @Project : thinking_in_java
 * @File : null.java
 * @Desc :
 */
public class Generators {
  /**
   * 根据生成器，给指定的集合填充n个值
   * @param coll
   * @param gen
   * @param n
   * @param <T>
   * @return
   */
  public static <T> Collection<T> fill(Collection<T> coll, Generator<T> gen, int n) {
    for (int i = 0; i < n; i++) {
      coll.add(gen.next());
    }
    return coll;
  }

  public static void main(String[] args) {
    Collection<Coffee> coffees = fill(new ArrayList<Coffee>(), new CoffeeGenerator(), 10);
    for (Coffee coffee : coffees) {
      System.out.println(coffee);
    }
  }
}
